package logical;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.JFrame;

import org.apache.commons.collections15.Transformer;

import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;

@SuppressWarnings("serial")
public class AutomataVisualizer implements Serializable {
	private DirectedSparseGraph<String, String> g;
	private Integer numVertex;
	private Integer numEdge;
	public VisualizationViewer<String, String> vv;
	private Transformer<String, String> transformer;
	private Transformer<String, Paint> transformerColor;
	public Vector<String> final_vertex;
	public Vector<String> getFinalVertex() {
		return final_vertex;
	}

	Map<String, String> mpEdge;
	public SetState currState = null;
	public JFrame frame;

	public AutomataVisualizer() {
		mpEdge = new HashMap<String, String>();
		g = new DirectedSparseGraph<String, String>();
		numVertex = 0;
		numEdge = 0;
		final_vertex = new Vector<String>();

	}

	public void view(int larg, int alt) {
		createView(larg, alt);

		frame = new JFrame();
		frame.getContentPane().add(vv);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
		frame.repaint();
	}

	public void createView(int larg, int alt) {
		vv = new VisualizationViewer<String, String>(
				new ISOMLayout<String, String>(g), new Dimension(larg, alt));
		transformer = new Transformer<String, String>() {
			@Override
			public String transform(String arg0) {
				return arg0.toString();
			}
		};
		vv.getRenderContext().setVertexLabelTransformer(transformer);
		/*transformerColor = new Transformer<String, Paint>() {
			public Paint transform(String i) {
				if (String.valueOf(currState.vertexNo).equals(i)) {
					if (final_vertex.contains(i))
						return (Paint) Color.ORANGE;
					else
						return (Paint) Color.YELLOW;
				} else if (final_vertex.contains(i))
					return (Paint) Color.RED;
				else {
					return (Paint) Color.GREEN;
				}
			}
		};*/
		//vv.getRenderContext().setVertexFillPaintTransformer(transformerColor);
		transformer = new Transformer<String, String>() {
			@Override
			public String transform(String arg0) {
				return mpEdge.get(arg0);
			}
		};
		vv.getRenderContext().setEdgeLabelTransformer(transformer);
		// vv.getRenderer().setVertexRenderer(new MyRenderer());

		// The following code adds capability for mouse picking of
		// vertices/edges. Vertices can even be moved!
		final DefaultModalGraphMouse<String, Number> graphMouse = new DefaultModalGraphMouse<String, Number>();
		vv.setGraphMouse(graphMouse);
		graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
	}

	public int addVertex() {
		g.addVertex(numVertex.toString());
		numVertex++;
		return numVertex - 1;
	}

	public void addEdge(String name, Integer source, Integer dest) {
		g.addEdge(numEdge.toString(), source.toString(), dest.toString());
		mpEdge.put(numEdge.toString(), name);
		numEdge++;
	}
}
